/*
带缓冲的通道
https://www.bilibili.com/video/BV1gf4y1r79E/?p=29&spm_id_from=pageDriver&vd_source=8bd7b24b38e3e12c558d839b352b32f4
*/
package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("channel with buffer")

	cb := make(chan int, 3) // 带有缓冲的channel
	fmt.Println("len(cb) = ", len(cb), ", cap(cb)", cap(cb))

	go func() {
		defer fmt.Println("子go程结束")

		for i := 0; i < 3; i++ {
			cb <- i
			fmt.Println("子go程正在运行，发送对的元素 = ", i, "len(cb) = ", len(cb), ", cap(cb) = ", cap(cb))
			time.Sleep(1 * time.Second)
		}
	}()

	for i := 0; i < 3; i++ {
		num := <-cb
		fmt.Println("main go程正在运行，接收对的元素 = ", num, "len(cb) = ", len(cb), ", cap(cb) = ", cap(cb))
		time.Sleep(3 * time.Second)
	}
}
